<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
		<style>
			
		</style>
		<script>
			// 做劫持用的
			function observer(data){
				Object.keys(data).forEach(function (item) {
					definedReactive(data,item,data[item])
				})	
			}
			//转成getter/setter
			function definedReactive(obj,key,value){
				Object.defineProperty(obj,key,{
					get(){
						return value;
					},
					set(newValue){
						value = newValue;
						// 渲染数据
						title.innerHTML = newValue;
					}
				})	
			}

		</script>
	</head>

	<body>
		<div id="app">
			<button id="btn">设置</button>
			<p id="title"></p>
		</div>
		<script>
			let title = document.querySelector('#title')
			let btn = document.querySelector('#btn')
			let data = {
				title: 'hello,测试',
				message: 'hello,测试'
			}

			// 劫持
			observer(data)

			title.innerHTML = data.title;

			btn.onclick = function (){
				data.title = '改变了'
			}

		</script>
	</body>
</html>